
package com.scalar.chiptrack.operations.process.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.scalar.ScalarException;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DataSource;
import com.scalar.chiptrack.operations.process.forms.DieBankForm;
import com.scalar.chiptrack.utils.SqlQueryMappings;


public class DieBankDAO {

    public static DieBankForm loadDieBankData (int processStepId) throws ScalarException {
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rset = null;
        
        DieBankForm dieBankData = null;
        
        try {
            conn = DataSource.getConnection();

            pstmt = conn.prepareStatement (SqlQueryMappings.get ("operations.dieBank.loadDieBankData"));
            pstmt.setInt (1, processStepId);
            
            rset = pstmt.executeQuery();
    
            if (rset.next()) {
                dieBankData = new DieBankForm (rset.getInt ("IST_Min_Level")+"",
                                               rset.getInt ("IST_Max_Level")+"",
                                               rset.getInt ("IST_Max_Time")+"",                                                        
                                               rset.getString ("Part_Number"),
                                               rset.getInt ("P_Parent_Product_ID"),
                                               rset.getString ("PS_Type"), 
                                               rset.getString ("PS_Comments"),
                                               rset.getInt ("previousStep_ID"));
            }
            if( dieBankData != null )
            {
				dieBankData.setDupProductName(ProductDao.loadProductDupNames( processStepId ));
            }
			
        }
        catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not LevelAccess list from database", sqle);
        }
        finally {
            if (rset != null) {
                try { rset.close(); } catch (SQLException ignore) {}
            }
            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore) {}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore) {}
            }
        }

        return dieBankData;
    }
    
    
    public static void storeDieBankData (DBTransaction dbTransaction, int processStepId, DieBankForm dbForm) throws ScalarException {
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;

        try {
            
            // Get the connection from regular datasource if its not in a manual transaction
            if (dbTransaction == null) {
                conn = DataSource.getConnection();
            }
            else {
                conn = dbTransaction.getConnection();
            }

            pstmt = conn.prepareStatement (SqlQueryMappings.get ("opearations.dieBank.insertNew"));
    
            pstmt.setInt (1, processStepId);
            pstmt.setInt (2, dbForm.getMinLevelAsInt());
            pstmt.setInt (3, dbForm.getMaxLevelAsInt());
            pstmt.setInt (4, dbForm.getMaxTimeAsInt());
            
            pstmt.executeUpdate();
            pstmt.close();
            pstmt = null;
            
            /*
            // Save locations
            String[] locations = dbForm.getLocations();
            
            if (locations != null) {
                pstmt = conn.prepareStatement(SqlQueryMappings.get("operations.process.dieBankLocation.insertLocations"));
                
                for (int i=0; i<locations.length; i++) {
            
                    pstmt.setInt(1, processStepId);
                    pstmt.setInt(2, Integer.parseInt(locations[i]));
                    pstmt.addBatch();
                }
                pstmt.executeBatch();
            }
            */
            
            success = true;
        }
        catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not store DieBank data", sqle);
        }
        finally {
            
            // Commit/Rollback here it selef -- if its not in a manual transaction
            if (dbTransaction == null) {
                try {
                    if (conn != null) {
                        if (success == true) {
                            conn.commit();
                        }
                        else {
                            conn.rollback();
                        }
                    }
                }
                catch (SQLException ignore) {}
            }

            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore){}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore){}
            }
        }   
    }

	//Update DieBank data by Raj Vallamsetla
	public static void updateDieBankData (DBTransaction dbTransaction, int processStepId, DieBankForm dbForm) throws ScalarException {
        
        Connection conn = null;
        PreparedStatement pstmt = null;
        boolean success = false;

        try {
            
            // Get the connection from regular datasource if its not in a manual transaction
            if (dbTransaction == null) {
                conn = DataSource.getConnection();
            }
            else {
                conn = dbTransaction.getConnection();
            }

            pstmt = conn.prepareStatement (SqlQueryMappings.get ("opearations.dieBank.updateOld"));
    
            pstmt.setInt (1, dbForm.getMinLevelAsInt());
            pstmt.setInt (2, dbForm.getMaxLevelAsInt());
            pstmt.setInt (3, dbForm.getMaxTimeAsInt());
			pstmt.setInt (4, processStepId);
            
            pstmt.executeUpdate();
            pstmt.close();
            pstmt = null;
            
                  
            success = true;
        }
        catch (Exception sqle) {
            sqle.printStackTrace();
            throw new ScalarException ("Could not store DieBank data", sqle);
        }
        finally {
            
            // Commit/Rollback here it selef -- if its not in a manual transaction
            if (dbTransaction == null) {
                try {
                    if (conn != null) {
                        if (success == true) {
                            conn.commit();
                        }
                        else {
                            conn.rollback();
                        }
                    }
                }
                catch (SQLException ignore) {}
            }

            if (pstmt != null) {
                try { pstmt.close(); } catch (SQLException ignore){}
            }
            if (conn != null) {
                try { conn.close(); } catch (SQLException ignore){}
            }
        }   
    }
   // Ends Here
    
}
